package com.example.dinner;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MyGridViewAdapter extends RecyclerView.Adapter<MyGridViewAdapter.MyHolder> {

    List<TV_MSG> datas;
    List<String> datas2;
    Context context;
    ItemClickListener mItemClickListener ;

    ArrayList<Integer> positinList=new ArrayList<>();
    int tag;


    public MyGridViewAdapter(Context context, List<TV_MSG> datas) {
        this.datas = datas;
        this.context = context;
    }
    public MyGridViewAdapter(Context context, List<String> datas2,int tag) {
        this.datas2 = datas2;
        this.context = context;
        this.tag=tag;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.name_item, parent, false);
//        MyHolder holder = new MyHolder(view);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
        if (tag==2) {
            holder.tv_msg.setText(datas2.get(position));
        }else {
            if (positinList!=null&&positinList.size()>0&&positinList.contains(position)) {
                holder.tv_msg.setText(datas.get(position).getText());
                holder.tv_msg.setBackgroundColor(Color.YELLOW);
            }else {
                holder.tv_msg.setText(datas.get(position).getText());
                holder.tv_msg.setBackgroundColor(datas.get(position).getColor());
            }
        }


//        if (datas.get(position).getColor()== Color.RED) {
//            holder.tv_msg.setText("1");
//            holder.tv_msg.setBackgroundColor(Color.GREEN);
//        }else {
//            holder.tv_msg.setText(datas.get(position).getText());
//            holder.tv_msg.setBackgroundColor(datas.get(position).getColor());
//        }

        // 点击事件一般都写在绑定数据这里，当然写到上边的创建布局时候也是可以的
        if (mItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 这里利用回调来给RecyclerView设置点击事件
                    mItemClickListener.onItemClick(position,v);
                }
            });
        }

        }

    @Override
    public int getItemCount() {
        if (tag==2) {
            return datas2==null?0:datas2.size();
        }else {
            return datas==null?0:datas.size();
        }

    }

    public class MyHolder extends RecyclerView.ViewHolder {

        TextView tv_msg;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            tv_msg = itemView.findViewById(R.id.tv_msg);
        }
    }

    public interface ItemClickListener{
        void onItemClick(int position,View view) ;
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener){
       mItemClickListener = itemClickListener ;

    }

    public void setPositinList(ArrayList<Integer> indexList) {
        positinList = indexList;
        notifyDataSetChanged();
    }
}
